package com.ruoyi.data.service.impl;

import com.ruoyi.data.domain.TdataHtWg;
import com.ruoyi.data.mapper.TdataHtWgMapper;
import com.ruoyi.data.service.ITdataHtWgService;
import com.ruoyi.display.domain.HourViolations;
import com.ruoyi.display.domain.Violations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 违规记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-05
 */
@Service
public class TdataHtWgServiceImpl implements ITdataHtWgService {

    @Autowired
    private TdataHtWgMapper tdataHtWgMapper;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 查询违规记录列表
     *
     * @param tdataHtWg 违规记录
     * @return 违规记录
     */
    @Override
    public List<TdataHtWg> selectTdataHtWgList(TdataHtWg tdataHtWg) {
        return tdataHtWgMapper.selectTdataHtWgList(tdataHtWg);
    }

    @Override
    public List<TdataHtWg> getStatistics(TdataHtWg tdataHtWg) {
        if (tdataHtWg.getDtList() != null) {
            if (tdataHtWg.getRange() != null && tdataHtWg.getRange().equals("1")) {
                String str = tdataHtWg.getDtList().get(1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = simpleDateFormat.parse(str);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                String dateFormatStr = simpleDateFormat.format(new Date(date.getTime() + 24 * 60 * 60 * 1000));
                tdataHtWg.getDtList().set(1, dateFormatStr);
            } else {
                ArrayList<String> dates = new ArrayList<>();
                dates.add(tdataHtWg.getDtList().get(0).substring(0, 7) + "-01");
                Integer yyyy = Integer.valueOf(tdataHtWg.getDtList().get(1).substring(0, 4));
                String mm = String.valueOf(Integer.valueOf(tdataHtWg.getDtList().get(1).substring(5, 7)) + 1);
                if (Integer.valueOf(mm) > 12) {
                    mm = "1";
                    yyyy = yyyy + 1;
                }
                if (Integer.valueOf(mm) < 10) {
                    mm = "0" + mm;
                }
                dates.add(yyyy + "-" + mm + "-01");
                tdataHtWg.setDtList(dates);
            }
        }
        return tdataHtWgMapper.getStatistics(tdataHtWg);
    }

    @Override
    public List<TdataHtWg> selectHtWgList(TdataHtWg tdataHtWg) {
        return tdataHtWgMapper.selectHtWgList(tdataHtWg);
    }

    @Override
    public void clear(List<String> roomIds) {
        tdataHtWgMapper.clear(roomIds);
    }

    @Override
    public void updateByRoomIdSelective(TdataHtWg record) {
        threadPoolTaskExecutor.execute(() -> tdataHtWgMapper.updateByRoomIdSelective(record));
    }

    @Override
    public List<Violations> todayViolations() {
        return tdataHtWgMapper.todayViolations();
    }

    @Override
    public Integer monthViolations() {
        return tdataHtWgMapper.monthViolations();
    }

    @Override
    public List<HourViolations> hourViolations(String date) {
        return tdataHtWgMapper.hourViolations(date);
    }

    @Override
    public void refresh(List<TdataHtWg> records) {
        if (!records.isEmpty()) {
            tdataHtWgMapper.refresh(records);
        }
    }

    @Override
    public TdataHtWg selectTdataHtWg(String roomId) {
        return tdataHtWgMapper.selectTdataHtWg(roomId);
    }

    @Override
    public void insert(TdataHtWg record) {
        tdataHtWgMapper.insert(record);
    }

    @Override
    public void insertSelective(TdataHtWg record) {
        tdataHtWgMapper.insertSelective(record);
    }

    @Override
    public void deleteByRoomIds(List<String> roomIds) {
        tdataHtWgMapper.deleteByRoomIds(roomIds);
    }
}
